package chap06;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class itemEvent implements ItemListener {
    public static void main(String[] args) {
        new itemEvent();
    }

    public itemEvent() {
        init();
    }

    public void init() {
        JFrame frame = new JFrame("itemEvent");
        JRadioButton radio1 = new JRadioButton("是");
        JRadioButton radio2 = new JRadioButton("否");
        ButtonGroup bg = new ButtonGroup();
        //区域框 添加按钮
        bg.add(radio1);
        bg.add(radio2);
        frame.setLayout(new FlowLayout());
        frame.add(radio1);
        frame.add(radio2);
        radio1.addItemListener(this);
        radio2.addItemListener(this);
        frame.setLocation(300, 400);
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        Object obj = e.getSource();//获得对象
        if (obj instanceof JRadioButton) {
            JRadioButton radioButton = (JRadioButton) obj;
            if (radioButton.isSelected()) {
                System.out.println(radioButton.getText() + "be selected");
            }
        }
    }
}

